package com.tbm.console.proj.service.impl;

import com.tbm.console.base.service.CommonServiceImpl;
import com.tbm.console.proj.entity.EquipmentEntity;
import com.tbm.console.proj.repository.EquipmentRepository;
import com.tbm.console.proj.service.EquipmentService;
import com.tbm.console.util.PageData;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.stereotype.Service;

import javax.persistence.criteria.CriteriaBuilder;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.Predicate;
import javax.persistence.criteria.Root;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

@Service
public class EquipmentServiceImpl extends CommonServiceImpl<EquipmentEntity> implements EquipmentService{

    @Autowired
    private EquipmentRepository equipmentRepository;

    @Override
    public List<EquipmentEntity> findAll(PageData pageData, Integer start, Integer length) {
        Sort sort = new Sort(Sort.Direction.ASC,"id");
        Pageable pageable = new PageRequest(start, length, sort);
        //使用 CriteriaBuilder方式来做过滤
        Specification<EquipmentEntity> sp = this.getSpecification(pageData);
        List<EquipmentEntity> all = equipmentRepository.findAll(sp,pageable).getContent();
        return all;
    }

    @Override
    public long count(PageData pageData) {
        return equipmentRepository.count(this.getSpecification(pageData));
    }

    @Override
    public List<EquipmentEntity> saveAll(List<EquipmentEntity> equipmentEntities) {
        return equipmentRepository.saveAll(equipmentEntities);
    }

    @Override
    public List<EquipmentEntity> findAllById(Integer[] ids) {
        List<EquipmentEntity> list = equipmentRepository.findAllById(Arrays.asList(ids));
        if(list.size()==0){
            EquipmentEntity equipmentEntity = new EquipmentEntity();
            list.add(equipmentEntity);
        }
        return list;
    }

    @Override
    public Specification<EquipmentEntity> getSpecification(PageData pageData) {
        return new Specification() {
            @Override
            public javax.persistence.criteria.Predicate toPredicate(Root root, CriteriaQuery criteriaQuery, CriteriaBuilder criteriaBuilder) {
                List<Predicate> preList = new ArrayList<Predicate>();
                if (null != pageData.get("deleteFlag")) {
                    preList.add(criteriaBuilder.equal(root.get("deleteFlag"), pageData.get("deleteFlag").toString()));
                }
                if (null != pageData.get("equipType")) {
                    preList.add(criteriaBuilder.equal(root.get("equipType"), pageData.get("equipType").toString()));
                }
                if (null != pageData.get("equipBelongTo")) {
                    preList.add(criteriaBuilder.equal(root.get("equipBelongTo"), pageData.get("equipBelongTo").toString()));
                }
                if (null != pageData.get("equipRentState")) {
                    preList.add(criteriaBuilder.equal(root.get("equipRentState"), pageData.get("equipRentState").toString()));
                }

                Predicate[] p = new Predicate[preList.size()];
                return criteriaBuilder.and(preList.toArray(p));
            }
        };
    }


}
